package ar.uba.fi.RSACryptoFiuba.view.encrypt;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import ar.uba.fi.RSACryptoFiuba.model.decrypt.RSADecryptMessage;
import ar.uba.fi.RSACryptoFiuba.model.keygeneration.RSAKeyGenerationMessage;

@SuppressWarnings("serial")
public class EncryptConfigurationPanel extends JPanel implements Observer {

	private JPanel configurationPanel;
	private JLabel publicKeyLabel;
	private JLabel eLabel;
    private JTextField eValue;
    private JLabel nLabel;
    private JTextField nValue;
    private JLabel messageLabel;
    private JTextField messageValue;
    private JButton useKeyButton;
    private JButton useMessageButton;
    	
    public EncryptConfigurationPanel(RSAKeyGenerationMessage keyGenerationMessage, RSADecryptMessage decryptMessage) {
    	super();
    	keyGenerationMessage.addObserver(this);
    	decryptMessage.addObserver(this);
        initComponents(keyGenerationMessage, decryptMessage);
    }
                         
    private void initComponents(final RSAKeyGenerationMessage keyGenerationMessage, final RSADecryptMessage decryptMessage) {

        configurationPanel = new JPanel();

        publicKeyLabel = new JLabel();
        publicKeyLabel.setFont(new Font("Ubuntu", 1, 18));
        publicKeyLabel.setText("Clave Pública:");
        
        eLabel = new JLabel();
        eLabel.setFont(new Font("Ubuntu", 1, 18));
        eLabel.setText("e:");

        nLabel = new JLabel();
        nLabel.setFont(new Font("Ubuntu", 1, 18));
        nLabel.setText("n:");

        messageLabel = new JLabel();
        messageLabel.setFont(new Font("Ubuntu", 1, 18));
        messageLabel.setText("Mensaje:");

        eValue = new JTextField();
        
        nValue = new JTextField();
        
        messageValue = new JTextField();        
        
        useKeyButton = new JButton();
        useKeyButton.setText("Usar Clave");
        useKeyButton.setEnabled(false);
        useKeyButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				
				eValue.setText(keyGenerationMessage.getE());
				nValue.setText(keyGenerationMessage.getN());
				
			}
		});
        
        useMessageButton = new JButton();
        useMessageButton.setText("Usar Mensaje");
        useMessageButton.setEnabled(false);
        useMessageButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				
				messageValue.setText(decryptMessage.getPlainText());
				
			}
		});
        
        eValue.addAncestorListener(new AncestorListener() {
			public void ancestorAdded(AncestorEvent event) {
				eValue.requestFocusInWindow();
			}

			public void ancestorRemoved(AncestorEvent event) {}

			public void ancestorMoved(AncestorEvent event) {}
		});
            
        GroupLayout configurationPanelLayout = new GroupLayout(configurationPanel);
        configurationPanel.setLayout(configurationPanelLayout);
        configurationPanelLayout.setHorizontalGroup(
            configurationPanelLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(configurationPanelLayout.createSequentialGroup()
                .addGap(160, 160, 160)
                .addGroup(configurationPanelLayout.createParallelGroup(Alignment.LEADING)
                    .addComponent(messageLabel)
                    .addGroup(configurationPanelLayout.createSequentialGroup()
                        .addGroup(configurationPanelLayout.createParallelGroup(Alignment.LEADING, false)
                            .addGroup(configurationPanelLayout.createSequentialGroup()
                                .addGroup(configurationPanelLayout.createParallelGroup(Alignment.LEADING)
                                    .addComponent(publicKeyLabel)
                                    .addGroup(configurationPanelLayout.createSequentialGroup()
                                        .addComponent(eLabel, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(ComponentPlacement.RELATED)
                                        .addComponent(eValue, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)))
                                .addGap(18, 18, 18)
                                .addComponent(nLabel, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addComponent(nValue, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
                            .addComponent(messageValue))
                        .addGap(26, 26, 26)
                        .addGroup(configurationPanelLayout.createParallelGroup(Alignment.LEADING, false)
                            .addComponent(useMessageButton, GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                            .addComponent(useKeyButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(212, Short.MAX_VALUE))
        );
        configurationPanelLayout.setVerticalGroup(
            configurationPanelLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(configurationPanelLayout.createSequentialGroup()
                .addComponent(publicKeyLabel)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(configurationPanelLayout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(eLabel)
                    .addComponent(eValue, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(nLabel)
                    .addComponent(nValue, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(useKeyButton))
                .addGap(11, 11, 11)
                .addComponent(messageLabel)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(configurationPanelLayout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(messageValue, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(useMessageButton))
                .addGap(0, 36, Short.MAX_VALUE))
        );
        
        
      GroupLayout layout = new GroupLayout(this);
      this.setLayout(layout);
      layout.setHorizontalGroup(
          layout.createParallelGroup(Alignment.LEADING)
          .addComponent(configurationPanel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      );
      layout.setVerticalGroup(
          layout.createParallelGroup(Alignment.LEADING)
          .addComponent(configurationPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      );
    }
    
    public String getEValue() {
		return eValue.getText();
	}

	public String getNValue() {
		return nValue.getText();
	}

	public String getMessageValue() {
		return messageValue.getText();
	}
	
	public void setEValue(String value) {
		this.eValue.setText(value);
	}

	public void setNValue(String value) {
		this.nValue.setText(value);
	}

	public void setMessageValue(String value) {
		this.messageValue.setText(value);
	}
	
	public void setErrors(){
		this.eValue.setBorder(BorderFactory.createLineBorder(Color.RED));
		this.nValue.setBorder(BorderFactory.createLineBorder(Color.RED));
		this.messageValue.setBorder(BorderFactory.createLineBorder(Color.RED));
	}
	
	public void resetErrors(){
		this.eValue.setBorder(UIManager.getBorder("TextField.border"));
		this.nValue.setBorder(UIManager.getBorder("TextField.border"));
		this.messageValue.setBorder(UIManager.getBorder("TextField.border"));
	}

	public void update(Observable o, Object arg) {
		if(o instanceof RSAKeyGenerationMessage){
			RSAKeyGenerationMessage keyGenerationMessage = (RSAKeyGenerationMessage) o; 
			useKeyButton.setEnabled(!keyGenerationMessage.getE().isEmpty() && !keyGenerationMessage.getN().isEmpty());
		}else{
			RSADecryptMessage decryptMessage = (RSADecryptMessage) o; 
			useMessageButton.setEnabled(!decryptMessage.getPlainText().isEmpty());
		}
	}
}
